package com.best.oasis.settlement.common.entity.security;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.GrantedAuthority;

import com.best.oasis.settlement.common.entity.base.BaseEntity;
import com.best.oasis.settlement.common.enums.security.RoleType;

/**
 * 权限，设置该系统的所有permission
 * 
 */
@Entity
@Table(name="ST_ROLE")
public class Role extends BaseEntity implements Serializable, GrantedAuthority {
    private static final long serialVersionUID = 3690197650654049848L;
    
    // @Id  @GeneratedValue(strategy=GenerationType.AUTO)
    //private Long id;
	/**
	 * 权限名称
	 */	
    @Column(length=100,unique=true,name="name")
    private String name;
	/**
	 * 权限描述说明
	 */	
    @Column(length=300,name="description")
    private String description;
	/**
	 * 当前权限的父权限
	 */	
    private Long parent;	
    
    
    /**
     * 数据权限：字段值
     */
    @Column(length=200,name="fieldValue")
    private String fieldValue;
    
    /**
     * 数据权限：字段名 
     */
    @Column(length=200,name="field")
    private String field;
    
    /**
     * 数据权限：对应的实体 
     */
    @Column(length=500,name="entity")
    private String entity;
    
    
    /**
     * 显示顺序
     */
    private Integer sortIndex;
    /**
     * 类型 
     */
    @Enumerated(EnumType.STRING)
    private RoleType roleType = RoleType.FUNCTION;
    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Role() {
    }

    /**
     * Create a new instance and set the name.
     * @param name name of the role.
     */
    public Role(final String name) {
        this.name = name;
    }

    /**
     * @see org.springframework.security.GrantedAuthority#getAuthority()
     * @return the name property (getAuthority required by Acegi's GrantedAuthority interface)
     */
    @Transient
    public String getAuthority() {
        return getName();
    }

    
    public String getName() {
        return this.name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getParent() {
		return parent;
	}

	public void setParent(Long parent) {
		this.parent = parent;
	}

	public String getFieldValue() {
		return fieldValue;
	}

	public void setFieldValue(String fieldValue) {
		this.fieldValue = fieldValue;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public RoleType getRoleType() {
		return roleType;
	}

	public void setRoleType(RoleType roleType) {
		this.roleType = roleType;
	}

	public Integer getSortIndex() {
		return sortIndex;
	}

	public void setSortIndex(Integer sortIndex) {
		this.sortIndex = sortIndex;
	}

	/**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Role)) {
            return false;
        }

        final Role role = (Role) o;

        return !(name != null ? !name.equals(role.name) : role.name != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (name != null ? name.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.name)
                .toString();
    }

    public int compareTo(Object o) {
        return (equals(o) ? 0 : -1);
    }
}
